/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.cggveritas.crewintranet.jms.dao.jpacontroller;

import fr.cggveritas.crewintranet.jms.dao.jpacontroller.exceptions.NonexistentEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import fr.cggveritas.crewintranet.jms.entities.CvModel;
import fr.cggveritas.crewintranet.jms.entities.CvTypemodel;
import fr.cggveritas.crewintranet.jms.entities.CvVehicule;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author Alassane
 */
public class CvTypemodelJpaController extends DaoImp implements Serializable {

    public CvTypemodelJpaController() {
        super();
    }
    public void create(CvTypemodel cvTypemodel) {
        if (cvTypemodel.getCvVehiculeList() == null) {
            cvTypemodel.setCvVehiculeList(new ArrayList<CvVehicule>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvModel cvModelIdmodel = cvTypemodel.getCvModelIdmodel();
            if (cvModelIdmodel != null) {
                cvModelIdmodel = em.getReference(cvModelIdmodel.getClass(), cvModelIdmodel.getIdmodel());
                cvTypemodel.setCvModelIdmodel(cvModelIdmodel);
            }
            List<CvVehicule> attachedCvVehiculeList = new ArrayList<CvVehicule>();
            for (CvVehicule cvVehiculeListCvVehiculeToAttach : cvTypemodel.getCvVehiculeList()) {
                cvVehiculeListCvVehiculeToAttach = em.getReference(cvVehiculeListCvVehiculeToAttach.getClass(), cvVehiculeListCvVehiculeToAttach.getIdvehicule());
                attachedCvVehiculeList.add(cvVehiculeListCvVehiculeToAttach);
            }
            cvTypemodel.setCvVehiculeList(attachedCvVehiculeList);
            em.persist(cvTypemodel);
            if (cvModelIdmodel != null) {
                cvModelIdmodel.getCvTypemodelList().add(cvTypemodel);
                cvModelIdmodel = em.merge(cvModelIdmodel);
            }
            for (CvVehicule cvVehiculeListCvVehicule : cvTypemodel.getCvVehiculeList()) {
                CvTypemodel oldTypemodelOfCvVehiculeListCvVehicule = cvVehiculeListCvVehicule.getTypemodel();
                cvVehiculeListCvVehicule.setTypemodel(cvTypemodel);
                cvVehiculeListCvVehicule = em.merge(cvVehiculeListCvVehicule);
                if (oldTypemodelOfCvVehiculeListCvVehicule != null) {
                    oldTypemodelOfCvVehiculeListCvVehicule.getCvVehiculeList().remove(cvVehiculeListCvVehicule);
                    oldTypemodelOfCvVehiculeListCvVehicule = em.merge(oldTypemodelOfCvVehiculeListCvVehicule);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(CvTypemodel cvTypemodel) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvTypemodel persistentCvTypemodel = em.find(CvTypemodel.class, cvTypemodel.getIdcvTypemodel());
            CvModel cvModelIdmodelOld = persistentCvTypemodel.getCvModelIdmodel();
            CvModel cvModelIdmodelNew = cvTypemodel.getCvModelIdmodel();
            List<CvVehicule> cvVehiculeListOld = persistentCvTypemodel.getCvVehiculeList();
            List<CvVehicule> cvVehiculeListNew = cvTypemodel.getCvVehiculeList();
            if (cvModelIdmodelNew != null) {
                cvModelIdmodelNew = em.getReference(cvModelIdmodelNew.getClass(), cvModelIdmodelNew.getIdmodel());
                cvTypemodel.setCvModelIdmodel(cvModelIdmodelNew);
            }
            List<CvVehicule> attachedCvVehiculeListNew = new ArrayList<CvVehicule>();
            for (CvVehicule cvVehiculeListNewCvVehiculeToAttach : cvVehiculeListNew) {
                cvVehiculeListNewCvVehiculeToAttach = em.getReference(cvVehiculeListNewCvVehiculeToAttach.getClass(), cvVehiculeListNewCvVehiculeToAttach.getIdvehicule());
                attachedCvVehiculeListNew.add(cvVehiculeListNewCvVehiculeToAttach);
            }
            cvVehiculeListNew = attachedCvVehiculeListNew;
            cvTypemodel.setCvVehiculeList(cvVehiculeListNew);
            cvTypemodel = em.merge(cvTypemodel);
            if (cvModelIdmodelOld != null && !cvModelIdmodelOld.equals(cvModelIdmodelNew)) {
                cvModelIdmodelOld.getCvTypemodelList().remove(cvTypemodel);
                cvModelIdmodelOld = em.merge(cvModelIdmodelOld);
            }
            if (cvModelIdmodelNew != null && !cvModelIdmodelNew.equals(cvModelIdmodelOld)) {
                cvModelIdmodelNew.getCvTypemodelList().add(cvTypemodel);
                cvModelIdmodelNew = em.merge(cvModelIdmodelNew);
            }
            for (CvVehicule cvVehiculeListOldCvVehicule : cvVehiculeListOld) {
                if (!cvVehiculeListNew.contains(cvVehiculeListOldCvVehicule)) {
                    cvVehiculeListOldCvVehicule.setTypemodel(null);
                    cvVehiculeListOldCvVehicule = em.merge(cvVehiculeListOldCvVehicule);
                }
            }
            for (CvVehicule cvVehiculeListNewCvVehicule : cvVehiculeListNew) {
                if (!cvVehiculeListOld.contains(cvVehiculeListNewCvVehicule)) {
                    CvTypemodel oldTypemodelOfCvVehiculeListNewCvVehicule = cvVehiculeListNewCvVehicule.getTypemodel();
                    cvVehiculeListNewCvVehicule.setTypemodel(cvTypemodel);
                    cvVehiculeListNewCvVehicule = em.merge(cvVehiculeListNewCvVehicule);
                    if (oldTypemodelOfCvVehiculeListNewCvVehicule != null && !oldTypemodelOfCvVehiculeListNewCvVehicule.equals(cvTypemodel)) {
                        oldTypemodelOfCvVehiculeListNewCvVehicule.getCvVehiculeList().remove(cvVehiculeListNewCvVehicule);
                        oldTypemodelOfCvVehiculeListNewCvVehicule = em.merge(oldTypemodelOfCvVehiculeListNewCvVehicule);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = cvTypemodel.getIdcvTypemodel();
                if (findCvTypemodel(id) == null) {
                    throw new NonexistentEntityException("The cvTypemodel with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvTypemodel cvTypemodel;
            try {
                cvTypemodel = em.getReference(CvTypemodel.class, id);
                cvTypemodel.getIdcvTypemodel();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The cvTypemodel with id " + id + " no longer exists.", enfe);
            }
            CvModel cvModelIdmodel = cvTypemodel.getCvModelIdmodel();
            if (cvModelIdmodel != null) {
                cvModelIdmodel.getCvTypemodelList().remove(cvTypemodel);
                cvModelIdmodel = em.merge(cvModelIdmodel);
            }
            List<CvVehicule> cvVehiculeList = cvTypemodel.getCvVehiculeList();
            for (CvVehicule cvVehiculeListCvVehicule : cvVehiculeList) {
                cvVehiculeListCvVehicule.setTypemodel(null);
                cvVehiculeListCvVehicule = em.merge(cvVehiculeListCvVehicule);
            }
            em.remove(cvTypemodel);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<CvTypemodel> findCvTypemodelEntities() {
        return findCvTypemodelEntities(true, -1, -1);
    }

    public List<CvTypemodel> findCvTypemodelEntities(int maxResults, int firstResult) {
        return findCvTypemodelEntities(false, maxResults, firstResult);
    }

    private List<CvTypemodel> findCvTypemodelEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(CvTypemodel.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public CvTypemodel findCvTypemodel(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(CvTypemodel.class, id);
        } finally {
            em.close();
        }
    }

    public int getCvTypemodelCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<CvTypemodel> rt = cq.from(CvTypemodel.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    public List<CvTypemodel> findByModel(CvModel cvModel) {
        EntityManager em = getEntityManager();
        try {
            String queryString = "select c from CvTypemodel c "
                    + "where c.cvModelIdmodel = :x";
            Query query = em.createQuery(queryString);
            query.setParameter("x", cvModel);

            return query.getResultList();
        } finally {
            em.close();
        }
    }
}
